//
// Created by 17775 on 2024/6/24.
//

#ifndef RAYCI_GRAPHCONTROL_H
#define RAYCI_GRAPHCONTROL_H

#include <QWidget>
#include <QDockWidget>
#include "../qcustomplot.h"


QT_BEGIN_NAMESPACE
namespace Ui { class GraphControl; }
QT_END_NAMESPACE

class GraphControl : public QDockWidget {
Q_OBJECT
    QCustomPlot *wavelengthPlot = nullptr;
    QMenu *m_contextMenu = nullptr;

public:
    explicit GraphControl(QWidget *parent = nullptr);

    ~GraphControl() override;
    static void setQCustomPlot(QCustomPlot *customPlot);

    template<class X, class Y>
    void refresh(const QVector<X>& xData, const QVector<Y>& yData) {
        QVector<QCPGraphData> *mData;
        mData = wavelengthPlot->graph(0)->data()->coreData();
        mData->clear();

        QCPGraphData newPoint;
        auto s = xData.size();
        for (decltype(s) i = 0; i < s; ++i)
        {
            newPoint.key = xData[i];
            newPoint.value = yData[i];
            mData->append(newPoint);
        }
        wavelengthPlot->replot(QCustomPlot::rpQueuedReplot);
    }

    void contextMenuEvent(QContextMenuEvent *event) override;

private:
    Ui::GraphControl *ui;
};


#endif //RAYCI_GRAPHCONTROL_H
